unit settings;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Buttons,
  ComCtrls, ExtCtrls, StdCtrls, Spin;

type

  { TfrmSettings }

  TfrmSettings = class(TForm)
    btnClearAllGoodsCount: TBitBtn;
    btnSave: TBitBtn;
    btnClose: TBitBtn;
    Label1: TLabel;
    lbledtExchangeRate: TLabeledEdit;
    memName: TMemo;
    PageControl: TPageControl;
    TabSheetNullBase: TTabSheet;
    TabSheetGeneral: TTabSheet;
    procedure btnClearAllGoodsCountClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure checkErrors(Sender: TObject);
    procedure lbledtExchangeRateKeyPress(Sender: TObject; var Key: char);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  frmSettings: TfrmSettings;

implementation

{$R *.lfm}

{ TfrmSettings }

uses main,ZDataset;

procedure TfrmSettings.checkErrors(Sender: TObject);
begin
  if ((lbledtExchangeRate.Text=EmptyStr)) then begin
    btnSave.Enabled:=False;
  end else begin
    btnSave.Enabled:=True;
  end;
end;

procedure TfrmSettings.lbledtExchangeRateKeyPress(Sender: TObject; var Key: char
  );
begin
  FrmMain.KeyVerifyFloat(Sender,Key);
end;

procedure TfrmSettings.btnSaveClick(Sender: TObject);
begin
  FrmMain.DBQuery.SQL.Text:='UPDATE settings SET value='+lbledtExchangeRate.Text+' WHERE id=2';
  FrmMain.ExchangeRate:=StrToFloat(lbledtExchangeRate.Text);
  FrmMain.DBQuery.ExecSQL;
  FrmMain.DBQuery.SQL.Text:='UPDATE settings SET value='''+memName.Lines.Text+''' WHERE id=3';
  FrmMain.entrepreneur:=memName.Lines.Text;
  FrmMain.DBQuery.ExecSQL;
end;

procedure TfrmSettings.btnClearAllGoodsCountClick(Sender: TObject);
var
 query: TZQuery;
var
  res:Integer;
begin
  res:=MessageDlg('','Встановити кількість в номенклатурі та на складах в "0" ?',mtConfirmation,mbYesNo,0);
  if (res=mrYes) then begin
    try
     query := TZQuery.Create(nil);
     query.Connection:=FrmMain.ZConnection;
    with query do begin
      SQL.Text := 'UPDATE nomenclatureonstorehouses SET quantity=0';
      ExecSQL;
      SQL.Text := 'UPDATE nomenclature SET quantity=0';
      ExecSQL;
    end;
    finally
     query.Free ;
     query:= nil;
    end;
  end;
end;

end.

